<template>
  <Page auto-content-height>
    <div :class="cn('bg-card h-full rounded-md')">
      <el-button type="primary" @click="VxeGridRef?.commitProxy('reload')">
        刷新
      </el-button>
      <VxeGrid ref="VxeGridRef" v-bind="gridOptions" class="px-4">
        <template #loading>
          <slot name="loading">
            <VbenLoading :spinning="true" />
          </slot>
        </template>
      </VxeGrid>
    </div>
  </Page>
</template>

<script lang="ts" setup>
import type { VxeGridProps } from 'vxe-table';

import { reactive } from 'vue';

import { Page } from '@vben/common-ui';
import { usePreferences } from '@vben/preferences';
import { cn } from '@vben/utils';

import {
  VxeButton,
  VxeCheckbox,
  VxeIcon,
  VxeInput,
  VxeLoading,
  VxeModal,
  VxeNumberInput,
  VxePager,
  VxeRadioGroup,
  VxeSelect,
  VxeTooltip,
  VxeUI,
  VxeUpload,
} from 'vxe-pc-ui';
import enUS from 'vxe-pc-ui/lib/language/en-US';
// 导入默认的语言
import {
  VxeColgroup,
  VxeColumn,
  VxeGrid,
  VxeTable,
  VxeToolbar,
} from 'vxe-table';

// 先导入样式，确保样式优先级
import 'vxe-pc-ui/styles/cssvar.scss';
import 'vxe-table/styles/cssvar.scss';

// 导入默认的语言
import zhCN from 'vxe-pc-ui/lib/language/zh-CN';

import { getPositionList } from '#/api/sys/position';

import { VbenLoading } from '../../../../../../packages/@core/ui-kit/shadcn-ui/src/components/spinner';

const VxeGridRef = useTemplateRef('VxeGridRef');

const preference = usePreferences();
const localMap = {
  'zh-CN': zhCN,
  'en-US': enUS,
};
watch(
  [() => preference.theme.value, () => preference.locale.value],
  ([theme, locale]) => {
    VxeUI.setTheme(theme === 'dark' ? 'dark' : 'light');
    VxeUI.setI18n(locale, localMap[locale]);
    VxeUI.setLanguage(locale);
  },
  {
    immediate: true,
  },
);
// 注册所有需要的组件到同一个VxeUI实例
VxeUI.component(VxeButton);
VxeUI.component(VxeCheckbox);
VxeUI.component(VxeIcon);
VxeUI.component(VxeInput);
VxeUI.component(VxeLoading);
VxeUI.component(VxeModal);
VxeUI.component(VxeNumberInput);
VxeUI.component(VxePager);
VxeUI.component(VxeRadioGroup);
VxeUI.component(VxeSelect);
VxeUI.component(VxeTooltip);
VxeUI.component(VxeUpload);

VxeUI.component(VxeTable);
VxeUI.component(VxeColumn);
VxeUI.component(VxeColgroup);
VxeUI.component(VxeGrid);
VxeUI.component(VxeToolbar);

interface RowVO {
  id: number;
  name: string;
  nickname: string;
  role: string;
  sex: string;
  age: number;
  address: string;
}

// 模拟删除API
const delApi = (_records: any[]) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      // 删除记录处理逻辑
      resolve({ code: 200, message: '删除成功' });
    }, 100);
  });
};

// 模拟保存API
const saveApi = (_records: any[]) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      // 保存记录处理逻辑
      resolve({ code: 200, message: '保存成功' });
    }, 100);
  });
};

const gridOptions = reactive<VxeGridProps<RowVO>>({
  border: true,
  showOverflow: true,
  keepSource: true,
  height: 500,
  printConfig: {},
  importConfig: {},
  exportConfig: {},
  columnConfig: {
    resizable: true,
  },
  pagerConfig: {
    enabled: true,
    pageSize: 15,
  },
  editConfig: {
    trigger: 'click',
    mode: 'row',
    showStatus: true,
  },
  rowConfig: {
    isHover: true, // 开启悬浮行高亮
  },
  toolbarConfig: {
    refresh: true,
    import: true,

    export: true,
    print: true,
    zoom: true,
    custom: true,
    buttons: [
      { name: '新增', code: 'myAdd', status: 'primary' },
      { name: '删除', code: 'myDel', status: 'error' },
      { name: '保存', code: 'mySave', status: 'success' },
    ],
  },
  proxyConfig: {
    response: {
      result: 'items',
      total: 'total',
      list: 'items',
    },
    ajax: {
      // 接收 Promise
      query: async (page: any) => {
        await new Promise((resolve) => setTimeout(resolve, 1000));
        const { currentPage, pageSize } = page.page;
        return await getPositionList({ page: currentPage, pageSize });
      },

      // body 对象： { removeRecords }
      delete: ({ body }) => {
        return delApi(body.removeRecords);
      },
      // body 对象： { insertRecords, updateRecords, removeRecords, pendingRecords }
      save: ({ body }) => {
        return saveApi(body.insertRecords);
      },
    },
  },

  columns: [
    { type: 'checkbox', width: 50 },
    { type: 'seq', width: 70 },
    {
      field: 'name',
      title: '名称',
    },
    {
      field: 'remark',
      title: '备注',
    },
    {
      field: 'sort',
      title: '排序',
    },
    {
      field: 'createdAt',
      title: '时间',
    },
    {
      field: 'status',
      title: '状态',
      showOverflow: true,
    },
  ],
});
</script>
<style lang="scss">
:root .vxe-grid {
  --vxe-ui-font-color: hsl(var(--foreground));
  --vxe-ui-font-primary-color: hsl(var(--primary));

  /* --vxe-ui-font-lighten-color: #babdc0;
  --vxe-ui-font-darken-color: #86898e; */
  --vxe-ui-font-disabled-color: hsl(var(--foreground) / 50%);

  /* base */
  --vxe-ui-base-popup-border-color: hsl(var(--border));
  --vxe-ui-input-disabled-color: hsl(var(--border) / 60%);

  /* --vxe-ui-base-popup-box-shadow: 0px 12px 30px 8px rgb(0 0 0 / 50%); */

  /* layout */
  --vxe-ui-layout-background-color: hsl(var(--background));
  --vxe-ui-table-resizable-line-color: hsl(var(--heavy));

  /* --vxe-ui-table-fixed-left-scrolling-box-shadow: 8px 0px 10px -5px hsl(var(--accent));
  --vxe-ui-table-fixed-right-scrolling-box-shadow: -8px 0px 10px -5px hsl(var(--accent)); */

  /* input */
  --vxe-ui-input-border-color: hsl(var(--border));

  /* --vxe-ui-input-placeholder-color: #8d9095; */

  /* --vxe-ui-input-disabled-background-color: #262727; */

  /* loading */
  --vxe-ui-loading-background-color: hsl(var(--overlay-content));

  /* table */
  --vxe-ui-table-header-background-color: hsl(var(--accent));
  --vxe-ui-table-border-color: hsl(var(--border));
  --vxe-ui-table-row-hover-background-color: hsl(var(--accent-hover));
  --vxe-ui-table-row-striped-background-color: hsl(var(--accent) / 60%);
  --vxe-ui-table-row-hover-striped-background-color: hsl(var(--accent));
  --vxe-ui-table-row-radio-checked-background-color: hsl(var(--accent));
  --vxe-ui-table-row-hover-radio-checked-background-color: hsl(
    var(--accent-hover)
  );
  --vxe-ui-table-row-checkbox-checked-background-color: hsl(var(--accent));
  --vxe-ui-table-row-hover-checkbox-checked-background-color: hsl(
    var(--accent-hover)
  );
  --vxe-ui-table-row-current-background-color: hsl(var(--accent));
  --vxe-ui-table-row-hover-current-background-color: hsl(var(--accent-hover));

  /* --vxe-ui-table-fixed-scrolling-box-shadow-color: rgb(0 0 0 / 80%); */
}

.vxe-pager {
  .vxe-pager--prev-btn:not(.is--disabled):active,
  .vxe-pager--next-btn:not(.is--disabled):active,
  .vxe-pager--num-btn:not(.is--disabled):active,
  .vxe-pager--jump-prev:not(.is--disabled):active,
  .vxe-pager--jump-next:not(.is--disabled):active,
  .vxe-pager--prev-btn:not(.is--disabled):focus,
  .vxe-pager--next-btn:not(.is--disabled):focus,
  .vxe-pager--num-btn:not(.is--disabled):focus,
  .vxe-pager--jump-prev:not(.is--disabled):focus,
  .vxe-pager--jump-next:not(.is--disabled):focus {
    color: hsl(var(--accent-foreground));
    background-color: hsl(var(--accent));
    border: 1px solid hsl(var(--border));
    box-shadow: 0 0 0 1px hsl(var(--border));
  }

  .vxe-pager--wrapper {
    display: flex;
    align-items: center;
  }

  .vxe-pager--sizes {
    margin-right: auto;
  }
}

.vxe-pager--wrapper {
  @apply justify-center md:justify-end;
}

.vxe-tools--operate {
  margin-right: 0.25rem;
  margin-left: 0.75rem;
}

.vxe-table-custom--checkbox-option:hover {
  background: none !important;
}

.vxe-toolbar {
  padding: 0;
}

.vxe-buttons--wrapper:not(:empty),
.vxe-tools--operate:not(:empty),
.vxe-tools--wrapper:not(:empty) {
  padding: 0.6em 0;
}

.vxe-tools--operate:not(:has(button)) {
  margin-left: 0;
}
</style>
